# -*- coding:utf-8 -*-

import logging
from openerp.osv import fields, osv
from openerp import tools
import datetime


__logger = logging.getLogger(__name__)

class pm_owner_complain(osv.osv):
    _name = 'pm.owner.complain'
    _description = 'Owner Complain'
    
    _columns = {
                'name':fields.char(string='投诉编号',required=True,size=32),
                'project_id':fields.many2one('pm.project',string='所属项目',ondelete='cascade'),
                'area_id':fields.many2one('pm.project.area',string='所属区域',ondelete='cascade',domain="[('project_id','=',project_id)]"),
                'building_id':fields.many2one('pm.project.building',string="所属楼栋",ondelete='cascade',domain="[('project_area_id','=',area_id)]"),
                'room_id':fields.many2one('pm.project.building.room',string="房间名称",domain="[('building_id','=',building_id)]"),
                'floor':fields.related('room_id','floor',type='integer',string='所在楼层'),
                'property_owner':fields.related('room_id','property_owner',type='many2one',string='业主',relation='res.partner'),
                'householder':fields.related('room_id','hourseholder',type='many2one',string='当前住户',relation='res.partner'),
                'complain_person':fields.many2one('res.partner',string='投诉人',required=True),
                'contact_number':fields.char(string='联系电话',size=16),
                'complain_date':fields.date(string="投诉日期",readonly=True),
                'state':fields.selection([('draft','提交'),('waiting','待受理'),('handling','受理中'),('finish','受理完毕')],string="状态",required=True,readonly=True),
                'receiver':fields.many2one('res.users',string='接待人'),
                'content':fields.text('投诉内容',required=True),
                'handle_person':fields.many2one('res.users',string='受理人',readonly=True),
                'handle_time':fields.datetime('受理时间',readonly=True),
                'handle_result':fields.text('受理结果'),
                'is_satisfied':fields.selection([('yes',"满意"),('no','不满意')],string='是否满意'),
                'owner_valuation':fields.text('业主评价'),
                'remark':fields.text('备注'),
                }
    _defaults = {
                 'state':lambda self,cr,uid,context: 'draft'
                 }
    def onchange_room_id(self, cr, uid, ids, room_id):
        result = {'value': {}}
        val = self.pool.get('pm.project.building.room').read(cr, uid, [room_id], ['property_owner'])
        result['value']['complain_person'] = val[0]['property_owner']
        return result
    def create(self, cr, uid, values, context=None):
        values['state'] = 'waiting'
        values['complain_date'] = datetime.date.today()
        return super(pm_owner_complain, self).create(cr, uid, values, context=context)
    def handle(self,cr,uid,ids,context=None):
        self.write(cr, uid, ids, {'state':'handling','handle_person':uid,'handle_time':datetime.datetime.now()}, context)
        return True
    def finish(self,cr,uid,ids,context=None):
        self.write(cr, uid, ids, {'state':'finish'}, context)
        return True
    
class pm_let_maintenance(osv.osv):
    _name = 'pm.let.maintenance'
    _description = 'Report for maintenance'
    
    _columns = {
                'name':fields.char(string='报修编号',required=True,size=32),
                'project_id':fields.many2one('pm.project',string='所述项目',ondelete='cascade'),
                'area_id':fields.many2one('pm.project.area',string='所属区域',ondelete='cascade',domain="[('project_id','=',project_id)]"),
                'building_id':fields.many2one('pm.project.building',string='所属楼栋',ondelete='cascade',domain="[('project_area_id','=',area_id)]"),
                'room_id':fields.many2one('pm.project.building.room',string='房间名称',domain="[('building_id','=',building_id)]"),
                'floor':fields.related('room_id','floor',type='integer',string='所在楼层'),
                'property_owner':fields.related('room_id','property_owner',type='many2one',string='业主',relation='res.partner'),
                'householder':fields.related('room_id','householder',type='many2one',string='当前住户',relation='res.partner'),
                'proposer':fields.many2one('res.partner',string='申请人',required=True),
                'contact_number':fields.char(string='联系电话',size=16),
                'application_date':fields.date(string='申请时间'),
                'state':fields.selection([('draft','提交'),('waiting','待审批'),('handling','审批通过'),('refuse','拒绝'),('finish','已验收')],string="状态",required=True,readonly=True),
                'content':fields.text('请修内容',required=True),
                'fee':fields.float(string='维修费用',digits=(16,2)),
                'check_person':fields.many2one('res.users',string='审批人',readonly=True),
                'check_time':fields.datetime('审批时间',readonly=True),
                'check_advise':fields.text('审批意见'),
                'construction_team':fields.char(string='施工单位',size=100),
                'team_leader':fields.char(string='负责人',size=64),
                'leader_number':fields.char(string='负责人电话',size=16),
                'start_time':fields.date('开始日期'),
                'end_time':fields.date('结束日期'),
                'inspect_person':fields.many2one('res.users',string='验收人',readonly=True),
                'inspect_time':fields.datetime('验收时间',readonly=True),
                'inspect_advise':fields.text('验收意见'),
                'remark':fields.text('备注')
                }
    _defaults = {
                 'state':lambda self,cr,uid,context: 'draft'
                 }
    def onchange_room_id(self, cr, uid, ids, room_id):
        result = {'value': {}}
        val = self.pool.get('pm.project.building.room').read(cr, uid, [room_id], ['property_owner'])
        result['value']['proposer'] = val[0]['property_owner']
        return result
    def create(self, cr, uid, values, context=None):
        values['state'] = 'waiting'
        values['application_date'] = datetime.date.today()
        return super(pm_let_maintenance, self).create(cr, uid, values, context=context)
    def refuse(self,cr,uid,ids,context=None):
        self.write(cr, uid, ids, {'state':'refuse','check_person':uid,'check_time':datetime.datetime.now()},context)
        return True
    def handle(self,cr,uid,ids,context=None):
        self.write(cr, uid, ids, {'state':'handling','check_person':uid,'check_time':datetime.datetime.now()},context)
        return True
    def finish(self,cr,uid,ids,context=None):
        self.write(cr, uid,ids, {'state':'finish','inspect_person':uid,'inspect_time':datetime.datetime.now()},context)
        return True
    
class pm_owner_checkhourse(osv.osv):
    _name ='pm.owner.checkhourse'
    _columns = {
        'name':fields.many2one('pm.parameter',string='验收项目',domain="[('category_code','=','YSXM')]"),
        'project_id':fields.many2one('pm.project',string='所述项目',ondelete='cascade'),
        'area_id':fields.many2one('pm.project.area',string='所属区域',ondelete='cascade',domain="[('project_id','=',project_id)]"),
        'building_id':fields.many2one('pm.project.building',string='所属楼栋',ondelete='cascade',domain="[('project_area_id','=',area_id)]"),
        'room_id':fields.many2one('pm.project.building.room',string='房间名称',domain="[('building_id','=',building_id)]"),
        'check_date':fields.date(string='验收日期'),
        'confirm_date':fields.date(string='确认日期'),
        'state':fields.selection([('yes','合格'),('no','不合格')],string='是否合格'),
        'check_man':fields.char(string='验收人员',size=100),
        'owner_advise':fields.text(string='业主建议'),
        'hoursemanager_advise':fields.text(string='房管员意见'),
        'remark':fields.text(string='补充备注'),
    }
pm_owner_checkhourse()

class pm_owner_additional(osv.osv):
    _name ='pm.owner.additional'
    _columns = {
        'name':fields.many2one('pm.parameter',string='验收项目',domain="[('category_code','=','YSXM')]"),
        'project_id':fields.many2one('pm.project',string='所述项目',ondelete='cascade'),
        'area_id':fields.many2one('pm.project.area',string='所属区域',ondelete='cascade',domain="[('project_id','=',project_id)]"),
        'building_id':fields.many2one('pm.project.building',string='所属楼栋',ondelete='cascade',domain="[('project_area_id','=',area_id)]"),
        'room_id':fields.many2one('pm.project.building.room',string='房间名称',domain="[('building_id','=',building_id)]"),
        'area':fields.float(string='加建面积'),
        'state':fields.selection([('normal','正常'),('stoped','停用')],string='加建状态'),
        'add_date':fields.date(string='加建日期'),
        'remark':fields.text(string='加建说明'),
    }
pm_owner_additional()


class pm_owner_decoration(osv.osv):
    _name = 'pm.owner.decoration'

    _columns = {
        'project_id':fields.many2one('pm.project',string='所述项目',ondelete='cascade'),
        'area_id':fields.many2one('pm.project.area',string='所属区域',ondelete='cascade',domain="[('project_id','=',project_id)]"),
        'building_id':fields.many2one('pm.project.building',string='所属楼栋',ondelete='cascade',domain="[('project_area_id','=',area_id)]"),
        'room_id':fields.many2one('pm.project.building.room',string='房间名称',domain="[('building_id','=',building_id)]"),
        'name':fields.char(string='申请人',size=100),
        'telephone':fields.char(string='联系电话',size=100),
        'application_date':fields.date(string='申请日期'),
        'decoration_content':fields.text(string='装修内容'),
        'protect_amount':fields.float(string='装修保证金'),
        'approve_comment':fields.text(string='审批意见'),
        'construction_organization':fields.many2one('res.partner',string='施工单位'),
        'construction_contact':fields.char(string='联系人',size=100),
        'construction_contact_telephone':fields.char(string='联系人电话',size=100),
        'construct_start_date':fields.date(string='施工开始日期'),
        'construct_end_date':fields.date(string='施工结束日期'),
        'check_man':fields.char(string='验收人',size=100),
        'check_date':fields.date(string='验收日期'),
        'damage':fields.float(string='违约金'),
        'check_remark':fields.text(string='备注'),
        'state':fields.selection([('inputed','待审批'),('approved','审批通过'),('notcheck','待验收'),('checked','已验收')],string='状态'),
    }
    _defaults = {
        'state':'inputed',
    }
pm_owner_decoration()
